--
-- Author: shunguo.chen
-- Date: 2017-12-18 16:17:55
--
-- MountNewView  获得新坐骑界面

local AvatarFactory = app:getClass("AvatarFactory")
local Armature = app:getClass("Armature")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountNewScene

local MOUNT_GET_STR = L("lua_code_text_293")

function ClassRef:init()
    self._ui["Panel/Panel_bg/Image_1"]:setContentSize(display.size)

    self._ui.Button_1:addClickEventListener(function()
        self._ui.Button_1:hide()
        self._ui.Button_2:hide()
        local pc = app:getInst("PhotoController")
        pc:snapshotNode(self)
        local act = cca.seq({cca.delay(0), cca.callFunc(function()
            -- UIHandler:getPopupUi():removeUIByKey(ClassRef:getViewName())
            pc:showPhotoViewShare()
            self._ui.Button_1:show()
            self._ui.Button_2:show()
        end), })

        self._ui.Button_1:runAction(act)
    end)
    self._ui.Button_2:addClickEventListener(function()
        executeFunc(self._onCloseView, self)
    end)

    self._starTimeline = self._ui["Panel/light"]:getTimeline()
    self._starTimeline:pause()

end

function ClassRef:show()
    self:setVisible(true)
    self._root:setVisible(false)

    local params = {
        armatureName = "getride",
        path = Res.MountNewEfcPath
    }
    local armature = Armature.create(params)
    armature:setPosition(display.cx, display.cy)
    self:addChild(armature, 2)

    armature:play("getride", 1, function(sender)
            sender:removeFromParent()
        end)
    armature:onFrameEvent(function(sender, name)
            if name == "all" then
                self._root:setVisible(true)
                local mount = AvatarFactory.createRideById(self._data.tid)
                if not mount then return end
                self._ui.mount:addChild(mount)
                local size = self._data:getSize() or 1
                mount:setScale(size)
                mount:play("ride_idle")
                mount:setPositionX(self._ui.mount:getContentSize().width/2)
            end
        end)

    AM.playSoundById(5153)
end

function ClassRef:refresh(mount, tipStr)
    self._data = mount
    self._ui.title:setString(string.format(MOUNT_GET_STR, mount:getName()))
    if tipStr then
        self._ui.text:setVisible(true)
        self._ui.text:setString(tipStr)
    else
        self._ui.text:setVisible(false)
    end
end

return ClassRef